package com.xuexi.demos.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xuexi.demos.entity.DTO.UserDTO;
import com.xuexi.demos.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper  extends BaseMapper<User> {
    // 查询用户名是否存在
    @Select("select * from user where username = #{username}")
    boolean selectusername(String username);
    // 插入用户
    @Insert("insert into user (username, nickname, avatar_url, role, random_code, password, status, created_at, updated_at) values (#{username}, #{nickname}, #{avatarUrl}, #{role}, #{randomCode}, #{password}, #{status}, #{createdAt}, #{updatedAt})")
    int insert(User user);
    // 登录
    @Select("select * from user where username = #{username} and password = #{password}")
    UserDTO login(@Param("username") String username, @Param("password") String password);
    // 更新用户状态
    @Update("UPDATE user SET status = #{param2} WHERE username = #{param1}")
    int updateStatus(String username, Integer status);
    // 获取所有用户名
    @Select("select username from user")
    List<String> getAllUsernames();
    // 获取所有在线用户
    @Select("SELECT username FROM user WHERE status = 1")
    List<String> getOnlineUsersFromDB();
    
    // 根据用户名获取用户信息
    @Select("SELECT * FROM user WHERE username = #{username}")
    UserDTO getUserByUsername(@Param("username") String username);
}